﻿#include "audio_stream.h"
#include "listener.h"

template<> std::set<Stream::Audio::Listener::Interface*>
        Base::Listener::Interface<Stream::Audio::Listener::Interface>::listener_ =
        std::set<Stream::Audio::Listener::Interface*>();

using namespace xcast;

class AudioStreamListener : public Base::Listener::Interface<Stream::Audio::Listener::Interface> {
public:
  static void OnAdd(const char* stream_id, bool up, uint64_t uin, bool add) {
    for (auto it : listener_) {
      if (up) {
        it->OnUpAudioStream(stream_id, add);
      } else {
        it->OnDownAudioStream(stream_id, uin, add);
      }
    }
  }
  
  static void OnUpdate(const char* stream_id, uint64_t uin, int state) {
    for (auto it : listener_) {
      it->OnAudioStreamUpdate(stream_id, uin, state);
    }
  }
  
};

namespace Stream {
namespace Audio {
namespace Listener {
void Add(Interface* listener) {
  AudioStreamListener::Add(listener);
}

void Remove(Interface* listener) {
  AudioStreamListener::Remove(listener);
}
}

namespace XcastEvent {
void OnAdd(const char* stream_id, bool up, uint64_t uin, bool add) {
  AudioStreamListener::OnAdd(stream_id, up, uin, add);
}

void OnUpdate(const char* stream_id, uint64_t uin, int state){
  AudioStreamListener::OnUpdate(stream_id, uin, state);
}
  
}

int Start(const char* channel_id, const char* stream_id, bool enable) {
  xcast_data data;
  data["enabled"] = enable;
  int32_t ret = xcast_core::set_property(XC_STREAM_ENABLED, channel_id, stream_id, data);
  return ret;
}
int GetState(const char* stream_id) {
  xcast_data state = xcast_core::get_property(XC_STREAM_STATE, "wemeet-channel", stream_id);
  return state.int32_val();
}

}
}
